package com.study.easyexcel.business.template;

import com.alibaba.excel.util.MapUtils;
import com.study.easyexcel.business.entity.TravelerQuery;
import com.study.easyexcel.common.function.AxisFunction;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 旅行者资产组合填充模板
 *
 * @author yuhuan
 * @date 2023/7/4
 */
public class TravelerAssetTemplate extends FlatGroupTemplate<TravelerQuery> {

    public static final String NATIONALITY_GROUP_NAME = "nationality";

    public static final String ASSET_GROUP_NAME = "asset";

    public static final String MULTIPLE_GROUP_NAME = "multiple";

    public static final String SPECIAL_GROUP_NAME = "special";

    public TravelerAssetTemplate(AxisFunction<TravelerQuery, Map<String, Object>> source) {
        super(source);
        this.initData();
    }

    @Override
    public List<String> includeColumn() {
        // 横轴包含的所有属性(每个列组都应该包含以下属性)
        return Arrays.asList("name", "assets", "process");
    }

    @Override
    public Map<String, String> groupByHorizontalAxis() {
        // 't' 表示本年度的组数据；'l' 表示去年的组数据
        Map<String, String> yearGroup = MapUtils.newHashMap();
        yearGroup.put("thisYear", "t");
        yearGroup.put("lastYear", "l");
        return yearGroup;
    }

    @Override
    public Map<String, Map<String, String>> groupByVerticalAxis() {
        HashMap<String, Map<String, String>> group = MapUtils.newHashMap();
        // 第一组横轴组（国籍组）
        Map<String, String> nationalityGroup = MapUtils.newHashMap();
        nationalityGroup.put("america", "美国");
        nationalityGroup.put("china", "中国");
        nationalityGroup.put("britain", "英国");
        nationalityGroup.put("france", "法国");
        group.put(NATIONALITY_GROUP_NAME, nationalityGroup);
        // 第二组横轴组（资产阶级组）
        Map<String, String> assetGroup = MapUtils.newHashMap();
        assetGroup.put("richPeople", "上层阶级");
        assetGroup.put("civilian", "中产阶级");
        assetGroup.put("poorPeople", "小资阶级");
        group.put(ASSET_GROUP_NAME, assetGroup);
        return group;
    }

    @Override
    public Map<String, Object> bodyFromHorizontalGroup(String subject) {
        Map<String, Object> rowMap = MapUtils.newHashMap();
        // 合并所有列组的数据块，形成完整的行数据
        for (String alias : this.xAxisGroups.values()) {
            TravelerQuery travelerQuery = new TravelerQuery();
            travelerQuery.setHorizontalAxis(alias);
            travelerQuery.setVerticalAxis(subject);
            rowMap.putAll(regionBlockBody(travelerQuery, alias));
            // 预留给空白单元格的样式填充占位符
            rowMap.put(getKey(alias, UniversalGroupStyle.STYLE_FILL_KEY), null);
        }
        return rowMap;
    }
}
